package com.bl.weilan.service.biz.report.impl;

import com.bl.weilan.common.ApiResponseCode;
import com.bl.weilan.common.CommonServiceException;
import com.bl.weilan.common.utils.UUIDUtils;
import com.bl.weilan.dao.model.ReportEntity;
import com.bl.weilan.dao.repository.ReportRepository;
import com.bl.weilan.service.biz.report.ReportService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.DelListBody;
import com.bl.weilan.service.vo.report.ReportAddVo;
import com.bl.weilan.service.vo.report.ReportListVo;
import com.bl.weilan.service.vo.report.ReportModVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Log4j2
public class ReportServiceImpl implements ReportService {
    @Autowired
    ReportRepository reportRepository;

    @Override
    public ReportEntity getReportById(String reportId) {
        ReportEntity reportEntity = reportRepository.findOne(reportId);
        return reportEntity;
    }

    @Override
    public ReportListVo getReportLists(Pageable pageable ){
        Page<ReportEntity> reportEntities;
        ReportListVo reportListVo = new ReportListVo();
        reportEntities = reportRepository.findByOrderByWeightDesc(pageable);


        reportListVo.setTotalCount(reportEntities.getTotalElements());
        reportListVo.setList(reportEntities.getContent());

        return reportListVo;
    }

    @Override
    public void addReport(ReportAddVo reportAddVo) {
        ReportEntity reportEntity = new ReportEntity();
        reportEntity.setId(UUIDUtils.getUUID());
        reportEntity.setCreateTime(new Date());
        reportEntity.setUpdateTime(new Date());
        BeanUtils.copyProperties(reportAddVo, reportEntity);
        reportRepository.save(reportEntity);

    }

    @Override
    public void del(DelBody id) {
        reportRepository.delete(id.getId());
    }

    @Override
    public void delList(DelListBody idList) {
        for(String id : idList.getList()) {
            reportRepository.delete(id);
        }
    }

    @Override
    public void modify(ReportModVo reportModVo){
        ReportEntity reportEntity = reportRepository.findOne(reportModVo.getId());

        if (reportEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应信息");
        }
        reportEntity.setUpdateTime(new Date());
        BeanUtils.copyProperties(reportModVo, reportEntity);
        reportRepository.save(reportEntity);
    }


}
